// worker.js
const http = require("http");
const logger = require("./logger.js");

module.exports = (port) => {
    // 设置进程标题
    process.title = "node-worker";
    // 启动http服务
    http
        .createServer((req, res) => {
            res.writeHead(200);
            if (req.url == '/close') {
                res.end("[close]pid:" + process.pid + ", ppid: " + process.ppid, );
                // process.exit(2);
                throw '13';
            }
            res.end("I am worker, pid: " + process.pid + ", ppid: " + process.ppid);
        }, ).listen(port, () => {
            process.send(`启动成功: ${process.pid}`);
        });
    // 捕获主进程信息
    process.on('message', (message, sendHandle) => {
        console.log(`子服务${process.pid}启动: ${message}`);
    });
    // 捕获异常
    process.on("uncaughtException", function (err) {
        logger.log(err);
        process.send({
            act: "suicide"
        });
    });


}